<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        <meta name="author" content="隐星魂 (Roy.Sun)">
        
        <link rel="shortcut icon" href="../../img/favicon.ico">
        <title>基本约定 - 玄武操作系统</title>
        <link href="../../css/bootstrap.min.css" rel="stylesheet">
        <link href="../../css/font-awesome.min.css" rel="stylesheet">
        <link href="../../css/base.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.3/styles/color-brewer.min.css">

        <script src="../../js/jquery-1.10.2.min.js" defer></script>
        <script src="../../js/bootstrap.min.js" defer></script>
        <script src="https://cdn.staticfile.org/highlight.js/10.0.3/highlight.min.js"></script>
        <script>hljs.initHighlightingOnLoad();</script> 
    </head>

    <body>
        <div class="navbar fixed-top navbar-expand-lg navbar-dark bg-primary">
            <div class="container">
                <a class="navbar-brand" href="../..">玄武操作系统</a>
                <!-- Expander button -->
                <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbar-collapse">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <!-- Expanded navigation -->
                <div id="navbar-collapse" class="navbar-collapse collapse">
                        <!-- Main navigation -->
                        <ul class="nav navbar-nav">
                            <li class="navitem">
                                <a href="../.." class="nav-link">介绍</a>
                            </li>
                            <li class="navitem">
                                <a href="../../license/" class="nav-link">开源协议</a>
                            </li>
                            <li class="dropdown active">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">应用手册 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../01.%E7%AE%80%E4%BB%8B/" class="dropdown-item">简介</a>
</li>
                                    
<li>
    <a href="../02.%E7%B3%BB%E7%BB%9F%E5%9F%BA%E6%9C%AC%E6%9E%B6%E6%9E%84/" class="dropdown-item">系统基本架构</a>
</li>
                                    
<li>
    <a href="../03.%E6%9E%84%E5%BB%BA%E7%B3%BB%E7%BB%9F/" class="dropdown-item">构建系统</a>
</li>
                                    
<li>
    <a href="../04.%E5%90%AF%E5%8A%A8%E6%B5%81%E7%A8%8B/" class="dropdown-item">启动流程</a>
</li>
                                    
<li>
    <a href="./" class="dropdown-item active">基本约定</a>
</li>
                                    
<li>
    <a href="../06.%E4%B8%AD%E6%96%AD%E6%8E%A7%E5%88%B6/" class="dropdown-item">中断控制</a>
</li>
                                    
<li>
    <a href="../07.%E8%B0%83%E5%BA%A6%E5%99%A8/" class="dropdown-item">调度器</a>
</li>
                                    
<li>
    <a href="../08.%E7%BA%BF%E7%A8%8B/" class="dropdown-item">线程</a>
</li>
                                    
<li>
    <a href="../09.%E7%94%B5%E6%BA%90%E7%AE%A1%E7%90%86/" class="dropdown-item">电源管理</a>
</li>
                                    
<li>
    <a href="../10.%E8%BD%AF%E4%BB%B6%E5%AE%9A%E6%97%B6%E5%99%A8/" class="dropdown-item">软件定时器</a>
</li>
                                    
<li>
    <a href="../11.%E9%94%81/" class="dropdown-item">锁</a>
</li>
                                    
<li>
    <a href="../12.%E5%90%8C%E6%AD%A5/" class="dropdown-item">同步</a>
</li>
                                    
<li>
    <a href="../13.%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/" class="dropdown-item">内存管理</a>
</li>
                                    
<li>
    <a href="../14.%E7%8E%84%E6%AD%A6C%E5%BA%93/" class="dropdown-item">玄武C库</a>
</li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">快速开发指南 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">S32k</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/s32k/1.XuanWuOS-envsetup-guide/" class="dropdown-item">开发环境搭建指南</a>
</li>
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/s32k/2.XuanWuOS-project-setup-guide/" class="dropdown-item">工程配置指南</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">Stm32</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/stm32/1.XuanWuOS-envsetup-guide/" class="dropdown-item">开发环境搭建指南</a>
</li>
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/stm32/2.XuanWuOS-project-setup-guide/" class="dropdown-item">工程配置指南</a>
</li>
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/stm32/3.XuanWuOS-xwbd-atkh743/" class="dropdown-item">正点原子阿波罗H743开发板上手指南</a>
</li>
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/stm32/4.XuanWuOS-xwbd-fk429m/" class="dropdown-item">反客F429核心板上手指南</a>
</li>
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/stm32/5.XuanWuOS-xwbd-atkf407core/" class="dropdown-item">正点原子F407核心板上手指南</a>
</li>
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/stm32/6.XuanWuOS-xwbd-atkf103core/" class="dropdown-item">正点原子F103核心板上手指南</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">Xwko</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/xwko/1.XuanWuKO-guide/" class="dropdown-item">玄武内核模块快速上手指南</a>
</li>
    </ul>
  </li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">规范 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../../%E8%A7%84%E8%8C%83/git-commit-specification/" class="dropdown-item">git commit规范</a>
</li>
                                    
<li>
    <a href="../../%E8%A7%84%E8%8C%83/version-branch-specification/" class="dropdown-item">分支与版本号</a>
</li>
                                </ul>
                            </li>
                        </ul>

                    <ul class="nav navbar-nav ml-auto">
                        <li class="nav-item">
                            <a href="#" class="nav-link" data-toggle="modal" data-target="#mkdocs_search_modal">
                                <i class="fa fa-search"></i> 搜索
                            </a>
                        </li>
                            <li class="nav-item">
                                <a rel="prev" href="../04.%E5%90%AF%E5%8A%A8%E6%B5%81%E7%A8%8B/" class="nav-link">
                                    <i class="fa fa-arrow-left"></i> 上一篇
                                </a>
                            </li>
                            <li class="nav-item">
                                <a rel="next" href="../06.%E4%B8%AD%E6%96%AD%E6%8E%A7%E5%88%B6/" class="nav-link">
                                    下一篇 <i class="fa fa-arrow-right"></i>
                                </a>
                            </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="container">
            <div class="row">
                    <div class="col-md-3"><div class="navbar-light navbar-expand-md bs-sidebar hidden-print affix" role="complementary">
    <div class="navbar-header">
        <button type="button" class="navbar-toggler collapsed" data-toggle="collapse" data-target="#toc-collapse" title="Table of Contents">
            <span class="fa fa-angle-down"></span>
        </button>
    </div>

    
    <div id="toc-collapse" class="navbar-collapse collapse card bg-secondary">
        <ul class="nav flex-column">
            
            <li class="nav-item" data-level="1"><a href="#_1" class="nav-link">基本约定</a>
              <ul class="nav flex-column">
            <li class="nav-item" data-level="2"><a href="#_2" class="nav-link">基本类型</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#id" class="nav-link">标识符（ID）</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#_3" class="nav-link">标准头文件</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#_4" class="nav-link">操作系统抽象层</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#_5" class="nav-link">单核与多核</a>
              <ul class="nav flex-column">
              </ul>
            </li>
              </ul>
            </li>
        </ul>
    </div>
</div></div>
                    <div class="col-md-9" role="main">

<h1 id="_1">基本约定</h1>
<h2 id="_2">基本类型</h2>
<ul>
<li><strong>xwu8_t</strong> 8位无符号整数</li>
<li><strong>xws8_t</strong> 8位有符号整数</li>
<li><strong>xwu16_t</strong> 16位无符号整数</li>
<li><strong>xws16_t</strong> 16位有符号整数</li>
<li><strong>xwu32_t</strong> 32位无符号整数</li>
<li><strong>xws32_t</strong> 32位有符号整数</li>
<li><strong>xwu64_t</strong> 64位无符号整数</li>
<li><strong>xws64_t</strong> 64位有符号整数</li>
<li><strong>xwsz_t</strong> 大小值（无符号）</li>
<li><strong>xwssz_t</strong> 大小值（有符号）</li>
<li><strong>xwstk_t</strong> 栈帧（无符号），必须与指针位宽一致</li>
<li><strong>xwptr_t</strong> 指针的整数值（无符号），必须与指针位宽一致</li>
<li><strong>xwreg_t</strong> 寄存器（无符号），必须与指针位宽一致</li>
<li><strong>xwsreg_t</strong> 寄存器（有符号），必须与指针位宽一致</li>
<li><strong>xwer_t</strong> 错误码（有符号），通常为负数</li>
<li><strong>xwsq_t</strong> 序列值/位置/偏移（无符号），必须与指针位宽一致</li>
<li><strong>xwssq_t</strong> 序列值/位置/偏移（有符号），必须与指针位宽一致</li>
<li><strong>xwsid_t</strong> ID（有符号），必须与指针位宽一致</li>
<li><strong>xwid_t</strong> ID（无符号），必须与指针位宽一致</li>
<li><strong>xwisr_t</strong> 中断向量（函数指针）</li>
<li><strong>xwirq_t</strong> 中断号（有符号），负的中断号表示系统异常，正的中断号表示外部中断</li>
<li><strong>xwpr_t</strong> 优先级（有符号）</li>
<li><strong>xwbmp_t</strong> 位图（无符号）</li>
<li><strong>xwtm_t</strong> 时间（64位有符号整数，单位：纳秒）</li>
<li>
<p><strong>xwlfq_t</strong> 无锁队列</p>
</li>
<li>
<p><strong>ctor_f</strong> 构造函数</p>
</li>
</ul>
<pre><code class="C">typedef void (* ctor_f)(void * /*obj*/);
</code></pre>

<ul>
<li><strong>dtor_f</strong> 析构函数</li>
</ul>
<pre><code class="C">typedef void (* dtor_f)(void * /*obj*/);
</code></pre>

<ul>
<li><strong>xwsc_f</strong> 系统调用</li>
</ul>
<pre><code class="C">typedef xws64_t (* xwsc_f)(void * /*arg*/, ...);
</code></pre>

<h2 id="id">标识符（ID）</h2>
<p>玄武OS使用标识符（ID）来表示一个内核对象，很多API都需要ID作为参数。</p>
<ul>
<li>在多核的OS应用中，会同时出现CPU-A正在删除对象，CPU-B正在访问对象的情况。
    此种情况有可能造成CPU-B访问野指针。解决的方法是内核为对象建立一个唯一的“ID”，
    访问对象时先通过ID查找对象，如果查找的结果为非空，内核会通过增加引用计数的
    方式来保护对象在访问期间不被CPU-A删除；</li>
<li>在玄武OS中，统一为对象抽象了这种“ID”，用数据类型 <strong>xwid_t</strong> 表示；</li>
<li>在玄武OS中，为每一个玄武OS内核对象建立了"ID —— 指针"关联表( <strong>TODO</strong> )；</li>
<li>在精简的单核RTOS应用中，直接使用指针的数值作为“ID”，避免查找的过程。</li>
</ul>
<blockquote>
<p><strong>题外话</strong>
引用计数是基于原子操作实现的，优点是用法简单，
缺点是频繁的原子操作会导致CPU性能下降。</p>
</blockquote>
<h2 id="_3">标准头文件</h2>
<p>标准头文件中包含了玄武OS的基本C/C++语言环境，包括：</p>
<ul>
<li>配置</li>
<li>编译器的宏</li>
<li>类型</li>
<li>错误码</li>
<li>基本信息</li>
</ul>
<pre><code class="C">#include &lt;xwos/standard.h&gt;
</code></pre>

<pre><code class="C++">#include &lt;xwos/standard.hxx&gt;
</code></pre>

<h2 id="_4">操作系统抽象层</h2>
<p>玄武OS拥有一套操作系统抽象层（XWOSAL）接口，XWOSAL可以把不同操作系统的API
抽象成统一的接口，可方便应用程序的移植。目前，OSAL已支持以下系统：</p>
<ul>
<li>玄武OS内核</li>
<li>Linux内核（详见工程<a href="https://gitee.com/starsoul/XuanWuKO">https://gitee.com/starsoul/XuanWuKO</a>）。</li>
</ul>
<p>操作系统抽象层的接口以"xwosal_"作为前缀，头文件位于xwos/osal中。</p>
<h2 id="_5">单核与多核</h2>
<p>玄武OS对多核系统的支持情况：</p>
<ul>
<li>单核(UP)：支持</li>
<li>对称多核(SMP)：支持</li>
<li>边界多核(BMP)：有限支持，用户可在SMP系统的基础上，把线程绑定在特定的CPU中运行。</li>
<li>不对称多核(AMP)：不支持，一般AMP系统需要把每个核视为UP单独处理。
    甚至可在不同的核上运行不同的操作系统。AMP系统通常需要高度定制。</li>
<li>可以在配置文件cfg/XuanWuOS.h中修改单核或多核的配置：<ul>
<li>当 <strong>XuanWuOS_CFG_CORE</strong> 配置为 <strong>smp</strong> ，cfg/xwos.h中起作用的是以 <strong>XWSMPCFG_</strong>
    作为前缀的配置， <strong>XWUPCFG_</strong> 作为前缀的配置无作用；</li>
<li>当 <strong>XuanWuOS_CFG_CORE</strong> 配置为 <strong>up</strong> ，cfg/xwos.h中起作用的是以 <strong>XWUPCFG_</strong>
    作为前缀的配置， <strong>XWSMPCFG_</strong> 作为前缀的配置无作用；</li>
<li>cfg/xwos.h中其他前缀的配置为SMP和UP共同拥有的配置。</li>
</ul>
</li>
<li>代码中一些与单核多核相关的命名规则解释：<ul>
<li>函数中的 <strong>_lc</strong> 后缀：只在”本地“CPU执行的函数，在单核系统中”本地“CPU指的是
    系统中存在的唯一的一个CPU；而在多核系统中指的是正在执行当前函数的CPU；</li>
<li>函数中的 <strong>_lic</strong> 后缀：只在”本地“CPU的中断中执行的函数。</li>
</ul>
</li>
</ul></div>
            </div>
        </div>

        <footer class="col-md-12">
            <hr>
            <p>Documentation built with <a href="https://www.mkdocs.org/">MkDocs</a>.</p>
        </footer>
        <script>
            var base_url = "../..",
                shortcuts = {"help": 191, "next": 78, "previous": 80, "search": 83};
        </script>
        <script src="../../js/base.js" defer></script>
        <script src="../../search/main.js" defer></script>

        <div class="modal" id="mkdocs_search_modal" tabindex="-1" role="dialog" aria-labelledby="searchModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="searchModalLabel">搜索</h4>
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            </div>
            <div class="modal-body">
                <p>
                    在这里所有文档：
                </p>
                <form>
                    <div class="form-group">
                        <input type="search" class="form-control" placeholder="搜索..." id="mkdocs-search-query" title="Type search term here">
                    </div>
                </form>
                <div id="mkdocs-search-results"></div>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div><div class="modal" id="mkdocs_keyboard_modal" tabindex="-1" role="dialog" aria-labelledby="keyboardModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="keyboardModalLabel">Keyboard Shortcuts</h4>
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            </div>
            <div class="modal-body">
              <table class="table">
                <thead>
                  <tr>
                    <th style="width: 20%;">Keys</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td class="help shortcut"><kbd>?</kbd></td>
                    <td>Open this help</td>
                  </tr>
                  <tr>
                    <td class="next shortcut"><kbd>n</kbd></td>
                    <td>Next page</td>
                  </tr>
                  <tr>
                    <td class="prev shortcut"><kbd>p</kbd></td>
                    <td>Previous page</td>
                  </tr>
                  <tr>
                    <td class="search shortcut"><kbd>s</kbd></td>
                    <td>Search</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

    </body>
</html>
